package com.lwj.liwanjia.view.activity.myindent.tuikuantuihuo;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.indent.TkAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.TkdetailBean;
import com.lwj.liwanjia.impl.TkDetailView;
import com.lwj.liwanjia.presenter.TkDetailPresenter;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.view.activity.login.LoginActivity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 退款申请状态详情
 */
public class ApplyStatusActivity extends BaseActivity implements TkDetailView {

    @BindView(R.id.rlv_tk)
    RecyclerView rlvTk;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_dianpu)
    TextView tvDianpu;
    @BindView(R.id.tv_orderid)
    TextView tvOrderid;
    @BindView(R.id.tv_wlhao)
    TextView tvWlhao;
    @BindView(R.id.tv_tk)
    TextView tvTk;
    @BindView(R.id.wuliu_layout)
    LinearLayout wuliuLayout;


    private TkAdapter adapter;
    private List<TkdetailBean.DataBean.ProcessBean> list = new ArrayList<>();
    private String phone;
    private String tradeid;
    private TkDetailPresenter detailPresenter;
    private String token;

    @Override
    protected void initView() {
        tvLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("退货退款");
        Intent intent = getIntent();
        phone = intent.getStringExtra("phone");
        tradeid = intent.getStringExtra("tradeid");
        LinearLayoutManager manager = new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rlvTk.setLayoutManager(manager);
        adapter = new TkAdapter(this, list);
        rlvTk.setAdapter(adapter);
        token = SpfUtils.getSpfUtils(this).getToken();
        detailPresenter = new TkDetailPresenter(this);
    }

    @Override
    protected void initData() {
        detailPresenter.getTkDeatil(token, tradeid);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_apply_status;
    }


    @OnClick({R.id.tv_left, R.id.tv_lianxikefu})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.tv_lianxikefu:
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:" + phone));
                startActivity(intent);
                break;
        }
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "数据加载失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getTkDetail(TkdetailBean tkdetailBean) {
        if (tkdetailBean.getState() == 0) {
            Toast.makeText(this, "访问失败", Toast.LENGTH_SHORT).show();
        } else if (tkdetailBean.getState() == 1) {
            tvDianpu.setText(tkdetailBean.getData().getShopname());
            tvOrderid.setText(tkdetailBean.getData().getTradeId());
            String logistics = tkdetailBean.getData().getLogistics();
            if (TextUtils.isEmpty(logistics)) {
                wuliuLayout.setVisibility(View.GONE);
            } else {
                wuliuLayout.setVisibility(View.VISIBLE);
                tvWlhao.setText(logistics);
            }
            tvTk.setText("￥" + tkdetailBean.getData().getTotalpay());
            list.clear();
            list.addAll(tkdetailBean.getData().getProcess());
            adapter.notifyDataSetChanged();
            Collections.reverse(list);
        }
    }
}
